package com.hddf.project.auth.service;

import java.security.PublicKey;
import java.util.List;

import org.kommii.encryption.RSAHandler;

import com.hddf.project.auth.dao.AuthorityDao_MongoDB;
import com.hddf.project.auth.dao.UserDao_MongoDB;
import com.hddf.project.auth.dao_impl.AuthorityDaoImpl_MongoDB;
import com.hddf.project.auth.dao_impl.UserDaoImpl_MongoDB;
import com.hddf.project.auth.pojos.Authority;
import com.hddf.project.auth.pojos.User;
import com.kommii.mongodb_utils.Mongodb_ConnPools;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;



public class MongoDB_Query {
	
	private static MongoDB_Query mongo_query = null;
	private static UserDao_MongoDB user_dao_mongodb = null;
	private static AuthorityDao_MongoDB auth_dao_mongodb = null;
	
	static {
		if(mongo_query==null){
			mongo_query = new MongoDB_Query();
		}
		if(user_dao_mongodb==null){
			user_dao_mongodb = new UserDaoImpl_MongoDB();
		}
		if(auth_dao_mongodb==null){
			auth_dao_mongodb = new AuthorityDaoImpl_MongoDB();
		}
	}
	
	public static MongoDB_Query getInstance(){
		return mongo_query;
	}
	
	public  User getUserByName(String username){
		DBCollection coll  = null;
		try{
			//String password = u.getPassword();
			//String passwd = MD5Handler.encrypt(password);
			//u.setPassword(passwd);
			coll = Mongodb_ConnPools.getMongoDB_Coll(User.mongodb_collection_name);
			User u = user_dao_mongodb.getUserByName(username, coll);
			return u; 
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public  User getUserByFullName(String fullname){
		DBCollection coll  = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll(User.mongodb_collection_name);
			User u = user_dao_mongodb.getUserByFullName(fullname, coll);
			return u;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public User getUserById(int id){
		DBCollection coll  = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll(User.mongodb_collection_name);
			User u = user_dao_mongodb.getUserById(id, coll);
			return u;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public  User getUserByUser_no(String user_no){
		DBCollection coll  = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll(User.mongodb_collection_name);
			User u = user_dao_mongodb.getUserByUser_No(user_no, coll);
			return u;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public DBObject getUserByAppToken(String app_token){
		DBCollection coll  = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll(User.mongodb_collection_name);
			DBObject u = user_dao_mongodb.getUserByAppToken(app_token, coll);
			return u;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 根据令牌获取用户的简要信息 (昵称,私钥,用户id,令牌截止时间)
	 * @param app_token
	 * @return
	 */
	public DBObject getSimpleUserByAppToken(String app_token){
		DBCollection coll  = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll(User.mongodb_collection_name);
			DBObject u = user_dao_mongodb.getSimpleUserByAppToken(app_token, coll);
			return u;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 *  根据令牌获取用户的公钥
	 * @param app_token
	 * @return
	 */
	public PublicKey getPubKeyByAppToken(String app_token){
		DBCollection coll  = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll(User.mongodb_collection_name);
			DBObject u = user_dao_mongodb.getPubKeyByAppToken(app_token, coll);
			String pk = (String)u.get("pubk");
			PublicKey pubk = RSAHandler.decodePublicKeyFromString(pk);
			return pubk;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public DBObject getUserByWebToken(String web_token){
		DBCollection coll  = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll(User.mongodb_collection_name);
			DBObject u = user_dao_mongodb.getUserByWebToken(web_token, coll);
			return u;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public DBObject getDBObjectUserById(int id){
		DBCollection coll  = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll(User.mongodb_collection_name);
			DBObject u = user_dao_mongodb.getDBObjectUserById(id, coll);
			return u;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 根据 web 令牌获取用户的简要信息 (用户名,aes_key,用户id,令牌截止时间)
	 * @param app_token
	 * @return
	 */
	public DBObject getSimpleUserByWebToken(String web_token){
		DBCollection coll  = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll(User.mongodb_collection_name);
			DBObject u = user_dao_mongodb.getSimpleUserByWebToken(web_token, coll);
			return u;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public List<DBObject> getUserList(String start,String limit,int isopen){
		DBCollection coll  = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll(User.mongodb_collection_name);
			List<DBObject> list = user_dao_mongodb.getUserList(start, limit, isopen, coll);
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public int getUserTotalCount(int isopen){
		DBCollection coll  = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll(User.mongodb_collection_name);
			int rs = user_dao_mongodb.getUserTotalCount(isopen, coll);
			return rs;
		}catch(Exception e){
			e.printStackTrace();
			return -1;
		}
	}
	
	/*************权限部分****************/
	
	/**
	 * 根据用户的权限id 获得权限列表
	 * @param ids
	 * @return
	 */
	public List<DBObject> getAuthByUser(String ids){
		DBCollection coll  = null;
		try{
			String[] array = ids.split(",");
			int size = array.length;
			int[] arr = new int[size];
			for(int i=0;i<array.length ;i++){
				arr[i] = Integer.parseInt(array[i]);
			}
			coll = Mongodb_ConnPools.getMongoDB_Coll(Authority.mongodb_collection_name);
			List<DBObject> list = auth_dao_mongodb.getAuthByIdArray(arr,coll);
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public List<DBObject> getAuthList(String start,String limit,int state){
		DBCollection coll  = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll(Authority.mongodb_collection_name);
			List<DBObject> list = auth_dao_mongodb.getAuthList(start, limit, state, coll);
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public int getAuthTotalCount(int state){
		DBCollection coll  = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll(Authority.mongodb_collection_name);
			int totalCount = auth_dao_mongodb.getAuthTotalCount(state, coll);
			return totalCount;
		}catch(Exception e){
			e.printStackTrace();
			return -1;
		}
	}
	
	
	


}
